package com.gxa.controller.HospitalController;


import com.gxa.service.EncyclopediaService;
import com.gxa.utils.ImageUpload;
import com.gxa.utils.R;
import com.gxa.vo.EncyclopediaVo;
import com.gxa.vo.EncyclopediaVoPic;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Api(tags = "健康百科管理模块相关接口")
@RestController
public class EncyclopediaController {


    @Autowired
    private EncyclopediaService encyclopediaService;


    @ApiOperation(value="查询接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("hos:sel")
    @GetMapping("/doc/QueryEncyclopedia")
    public R QueryEncyclopedia(@ApiParam(name="标题",value = "根据标题查询",required = true)String title){
        System.out.println(title);
        List<EncyclopediaVo> encyclopediaVos = this.encyclopediaService.queryEncyclopediaByName(title);

        System.out.println(encyclopediaVos);
        return R.ok().put("data",encyclopediaVos);

    }
    @ApiOperation(value="修改前查询接口",notes="查询标题、作者、内容、状态、时间",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("hos:sel")
    @PostMapping("/doc/PreEditEncyclopedia")
    public R PreEditEncyclopedia(@ApiParam(name="标题",value = "根据标题查询",required = true)@RequestBody EncyclopediaVo encyclopediaVo){

        encyclopediaVo.setId(this.encyclopediaService.queryEncyclopediaId(encyclopediaVo));
        //List<EncyclopediaVo> encyclopediaVos = this.encyclopediaService.queryEncyclopedia(encyclopediaVo);


        System.out.println(encyclopediaVo);

        return R.ok().put("data",encyclopediaVo);

    }

    @ApiOperation(value="修改接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("hos:update")
    @PutMapping("/doc/EditEncyclopedia")
    public R EditEncyclopedia(@ApiParam(name="修改后的数据",value = "根据修改后的数据修改",required = true)@RequestBody EncyclopediaVo encyclopediaVo){

        this.encyclopediaService.updateEncyclopedia(encyclopediaVo);


        System.out.println(encyclopediaVo);
        return R.ok();

    }



    @ApiOperation(value="添加接口",notes="添加",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("hos:add")
    @PostMapping("/doc/addEncyclopedia")
    public R addEncyclopedia(@ApiParam(name="添加内容",value = "添加内容",required = true)@RequestBody EncyclopediaVo encyclopediaVo){

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        encyclopediaVo.setTime(sdf.format(new Date()));
        this.encyclopediaService.insertEncyclopedia(encyclopediaVo);

        System.out.println(encyclopediaVo);
        return R.ok();

    }

    @ApiOperation(value="删除接口",notes="删除",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("hos:del")
    @DeleteMapping("/doc/DelEncyclopedia")
    public R DelEncyclopedia(@ApiParam(name="删除",value = "删除",required = true)@RequestBody EncyclopediaVo encyclopediaVo){

        this.encyclopediaService.deleteEncyclopedia(encyclopediaVo.getTitle());

        System.out.println(encyclopediaVo);
        return R.ok();

    }
    @ApiOperation(value="无参查询接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @RequiresPermissions("hos:sel")
    @PostMapping("/doc/QueryEncyclopedia1")
    public R QueryEncyclopedia1(){

        List<EncyclopediaVo> encyclopediaVos = this.encyclopediaService.queryEncyclopediaAll();
        return R.ok().put("data",encyclopediaVos);

    }


    @ApiOperation(value="健康百科图片上传接口",notes="方法的详细描述",produces = "application/json",response = R.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header",dataType ="String" ,required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回查询详细信息",examples = @Example(
                    value = {
                            @ExampleProperty(value = "{'code':200,'msg':'描述'}}", mediaType = "application/json")
                    })),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })

    @PostMapping("/EncyclopediaPic")

    public R EncyclopediaPic(@ModelAttribute @Validated EncyclopediaVoPic encyclopediaVo) throws IOException {
        MultipartFile pic = encyclopediaVo.getUrl();//获取传过来的图片文件
        System.out.println("##############################################");
        String url = ImageUpload.upload(pic);//传过来的文件放在工具类里面处理
        System.out.println(url);

        return R.ok().put("data",url);
    }
}
